﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Windows.Forms;
using HalconDotNet;
using System.Drawing.Imaging;

namespace MyLearn
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //创建图象，保存将来截取的图象
        Bitmap image = null;
        Graphics imgGraphics = null;
        HObject ho_Image = null;
        HalconDotNet.HWindowControl hWindowControlWPF4Continus = null;
        int nWidth, nHeight;

        HTuple hv_ModelID = null, hv_Row = null, hv_Column = null;
        HTuple hv_Angle = null, hv_ScaleR = null, hv_ScaleC = null;
        HTuple hv_Score = null;


        /// <summary>
        /// 采图线程
        /// </summary>
        private System.Threading.Thread grabImageThread = null;

        public MainWindow()
        {
            InitializeComponent();

            //设置Halcon控件大小-定义图像
            hWindowControlWPF4Continus = new HWindowControl() { Width = 400, Height = 300 };
            windowsFormsHost4Continus.Height = hWindowControlWPF4Continus.Height;
            windowsFormsHost4Continus.Width = hWindowControlWPF4Continus.Width;
            windowsFormsHost4Continus.Child = hWindowControlWPF4Continus;

            //创建图象，保存将来截取的图象
            image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            imgGraphics = Graphics.FromImage(image);
            nWidth = Screen.PrimaryScreen.Bounds.Width;
            nHeight = Screen.PrimaryScreen.Bounds.Height;
            HOperatorSet.SetPart(hWindowControlWPF4Continus.HalconWindow, 0, 0, nWidth - 1, nHeight - 1);

            HOperatorSet.ReadShapeModel("bofang.shm", out hv_ModelID);
        }





        int sleepTimer = 1000;

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                sleepTimer = Convert.ToInt32(txtTime.Text);
            }
            catch
            {
                sleepTimer = 10000;
            }

            if (sleepTimer < 1000)
                sleepTimer = 1000;

            txtTime.Text = sleepTimer.ToString();

            //4.开启相机采集线程
            grabImageThread = new System.Threading.Thread(GrabImageFunc);
            grabImageThread.IsBackground = true;
            grabImageThread.Priority = System.Threading.ThreadPriority.Normal;
            grabImageThread.Start();


            btnStart.IsEnabled = false;
            btnStop.IsEnabled = true;
        }


        bool isGrabImage = false;
        private void GrabImageFunc()
        {
            isGrabImage = true;

            while (isGrabImage)
            {
                //设置截屏区域
                imgGraphics.CopyFromScreen(0, 0, 0, 0, new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));

                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, image.Width, image.Height);
                BitmapData srcBmpData = image.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                HOperatorSet.GenImageInterleaved(out ho_Image, srcBmpData.Scan0, "bgr", image.Width, image.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
                image.UnlockBits(srcBmpData);

                HOperatorSet.FindAnisoShapeModel(ho_Image, hv_ModelID, -0.39, 0.78, 0.9, 1.1,
             0.9, 1.1, 0.5, 1, 0.5, "least_squares", 0, 0.9, out hv_Row, out hv_Column,
             out hv_Angle, out hv_ScaleR, out hv_ScaleC, out hv_Score);

                if ((int)(new HTuple((new HTuple(hv_Score.TupleLength())).TupleEqual(1))) != 0)
                {
                    if ((int)(new HTuple(((hv_Score.TupleSelect(0))).TupleGreater(0.8))) != 0)
                    {
                        //显示暂停了
                        MouseHelper.SetCursorPos((int)hv_Column.D, (int)hv_Row.D);
                        MouseHelper.mouse_event(MouseHelper.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                        MouseHelper.mouse_event(MouseHelper.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                       
                    }
                }


                HOperatorSet.DispObj(ho_Image, hWindowControlWPF4Continus.HalconWindow);
                ho_Image.Dispose();


                System.Threading.Thread.Sleep(sleepTimer);
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            isGrabImage = false;

            if (grabImageThread != null && grabImageThread.IsAlive)
            {
                grabImageThread.Abort();
                grabImageThread.Join(5000);
            }

            btnStop.IsEnabled = false;
            btnStart.IsEnabled = true;
        }


    }
}
